﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Identity;
using TeamPrasioliteWarGame.Models;
using System.Collections.Generic;
using TeamPrasioliteWarGame.Controls.ErrorSuccessNotifier;

namespace TeamPrasioliteWarGame
{
    public partial class UnitsView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Entities context = new Entities();

            string userGuidId = User.Identity.GetUserId();

            var user = context.ExtendUsers.Include("AspNetUser").FirstOrDefault(u => u.UserId == userGuidId);
            var userBuildings = new List<UserBuilding>();
            var userBuildingsString = new List<string>();

            if (user != null)
            {
                userBuildings = user.AspNetUser.UserBuildings.ToList();
                userBuildingsString = userBuildings.Select(ub => ub.Building.Type).ToList();
            }

            this.UserBuildingsView.DataSource = userBuildings;
            this.UserBuildingsView.DataBind();

            var allBuildings = context.Buildings.Where(b => !userBuildingsString.Contains(b.Type)).ToList();

            this.AllBuldingsView.DataSource = allBuildings;
            this.AllBuldingsView.DataBind();
        }

        protected void UpgradeBuilding_Command(object sender, CommandEventArgs e)
        {
            Entities context = new Entities();

            int buildingId = Convert.ToInt32(e.CommandArgument.ToString());

            var building = context.Buildings.FirstOrDefault(b => b.Id == buildingId);

            if (building != null)
            {
                string userGuidId = User.Identity.GetUserId();
                var user = context.ExtendUsers.Include("AspNetUser").FirstOrDefault(u => u.UserId == userGuidId);
                var currentBuilding = user.AspNetUser.UserBuildings.FirstOrDefault(ub => ub.BuildingId == buildingId);

                if (currentBuilding != null)
                {
                    var coeficient = Math.Pow(1.267, currentBuilding.BuildingLevel);
                    var woodCost = (int)(currentBuilding.Building.WoodCost * coeficient);
                    var ironCost = (int)(currentBuilding.Building.IronCost * coeficient);

                    if (user.Wood - woodCost >= 0 && user.Iron - ironCost >= 0)
                    {
                        user.Wood -= woodCost;
                        user.Iron -= ironCost;

                        currentBuilding.BuildingLevel++;
                        context.SaveChanges();

                        ErrorSuccessNotifier.AddSuccessMessage("You successfuly upgraded " + currentBuilding.Building.Type + " to level " + currentBuilding.BuildingLevel);
                        Response.Redirect("~/Buildings.aspx");
                    }
                    else
                    {
                        ErrorSuccessNotifier.AddErrorMessage("Not enough resourses !");
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("You can't upgrade building that you don't have !");
                }
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage("The building does not exists !");
            }
        }

        protected void CreateBuilding_Command(object sender, CommandEventArgs e)
        {
            Entities context = new Entities();

            int buildingId = Convert.ToInt32(e.CommandArgument.ToString());
            
            var building = context.Buildings.FirstOrDefault(b => b.Id == buildingId);

            if (building != null)
            {
                string userGuidId = User.Identity.GetUserId();
                var user = context.ExtendUsers.Include("AspNetUser").FirstOrDefault(u => u.UserId == userGuidId);
                var currentBuildingExist = user.AspNetUser.UserBuildings.FirstOrDefault(ub => ub.BuildingId == buildingId);

                if (currentBuildingExist == null)
                {
                    if (user.Wood - building.WoodCost >= 0 && user.Iron - building.IronCost >= 0)
                    {
                        var newUserBuilding = new UserBuilding();
                        newUserBuilding.BuildingLevel = 1;
                        newUserBuilding.BuildingId = buildingId;
                        newUserBuilding.AspNetUser = user.AspNetUser;

                        user.AspNetUser.UserBuildings.Add(newUserBuilding);

                        user.Wood -= building.WoodCost;
                        user.Iron -= building.IronCost;

                        context.SaveChanges();

                        ErrorSuccessNotifier.AddSuccessMessage("You successfuly built " + building.Type + " level 1");
                        Response.Redirect("~/Buildings.aspx");
                    }
                    else
                    {
                        ErrorSuccessNotifier.AddErrorMessage("Not enough resourses !");
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddInfoMessage("You already have that building!");
                }
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage("The building does not exists !");
            }
        }
    }
}